package com.infoland.afanmall.base.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;

import com.infoland.afanmall.base.util.LogKit;

/**
 * @author 王永吉
 * @date 2018/8/1 13:34
 * 懒加载Fragment
 */
public abstract class BaseLazyFragment extends BaseFragment {
    boolean viewIsInit = false;
    boolean viewIsShow = false;
    boolean isLoad = false;
    public String TAG = "BaseLazyFragment";

    //初始化数据
    public abstract void initData();

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        viewIsInit = true;
        isLoadData();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        viewIsShow = isVisibleToUser;
        isLoadData();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        viewIsInit = false;
        viewIsShow = false;
        isLoad = false;
    }

    public synchronized void isLoadData() {
        //    是够已经执行过 加载数据
        if (isLoad) {
//            LogKit.E("aaaa ---->   " + TAG, "  数据已经初始化完毕");
        } else {
            //    是否已经初始化完毕，视图 ，，，当前fragment 是否展示
            if (viewIsInit && viewIsShow) {
                isLoad = true;
                initData();
//                LogKit.E("aaaa ---->   " + TAG, "  开始执行初始化数据");
            } else {
//                LogKit.E("aaaa ---->   " + TAG, "  不符合执行条件");
            }
        }
    }
}
